package exam.axhu.action;

import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
import exam.axhu.entity.Student;
import exam.axhu.entity.Teacher;
import exam.axhu.entity.User;
import exam.axhu.service.StudentService;
import exam.axhu.service.TeacherService;


@SuppressWarnings("serial")
@Controller @Scope("prototype")
public class SignUp extends ActionSupport{
//defaultClass: signUp
	@Resource TeacherService teacherService;
	@Resource StudentService studentService;
	private User user;
	private Teacher teacher;
	private Student student;
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	
	public String up(){
		if(user.getUserID().length()!=0 && user.getPassword().length()!=0 && user.getName().length()!=0 && user.getType()!=null)
		{
			if(!user.getRepassword().equals(user.getPassword()))
			{
				addActionMessage("两次输入的密码不一致!");
				return "SignUpFailed";
			}
			
			if(user.getType().equals("T")){
				teacher=new Teacher(user.getUserID(),user.getName(),user.getPassword());
				if(teacherService.find(teacher.getTeacherID())==null)
				{	
					teacher.setJoinDate(new java.sql.Date(new java.util.Date().getTime()));
					teacherService.save(teacher);
					addActionMessage("注册成功!欢迎!");
					return "SignUpSucceed";
				}
				else {
					addActionMessage("系统检测到您之前已经注册过!欢迎登录~(请确保登录密码与之前相符)");
					return "SignUpSucceed";
				}
			}
			
			if(user.getType().equals("S")){
				student=new Student(user.getUserID(),user.getName(),user.getPassword());
				if(studentService.find(student.getStudentID())==null)
				{	
					student.setJoinDate(new java.sql.Date(new java.util.Date().getTime()));
					studentService.save(student);
					addActionMessage("注册成功!欢迎!");
					return "SignUpSucceed";
				}
				else {
					addActionMessage("系统检测到您之前已经注册过!欢迎登录~(请确保登录密码与之前相符)");
					return "SignUpSucceed";
				}
			}
		}
		
		addActionError("用户名、密码、姓名、身份不能为空!");
		return "SignUpFailed";
		

	}

	public String directup(){
		return "SignUpFailed";
	}
	
}
